local sugar = SUGAR_webber_the_trainer

-- 龙脊焚劫烩
AddPrefabPostInit('webbert_dish_dragoninfernostew',function (inst)
    if inst.Light == nil then
        inst.entity:AddLight()
        inst.Light:SetFalloff(.7)
        inst.Light:SetIntensity(.7)
        inst.Light:SetRadius(5)
        inst.Light:SetColour(253/255, 137/255, 164/255)
        inst.Light:Enable(false)
    end

    if not TheWorld.ismastersim then
        return inst
    end

    if inst.components.cooker == nil then
        inst:AddComponent("cooker")
    end

    if inst.components.heater == nil then
        inst:AddComponent("heater")
		inst.components.heater.carriedheat = 50
        inst.components.heater.heat = 70
    end

    if inst.components.inventoryitem then
        local old_onputininventoryfn = inst.components.inventoryitem.onputininventoryfn
        inst.components.inventoryitem.onputininventoryfn = function (this, owner, ...)
            inst.Light:Enable(false)
            sugar:removeEntRefer(inst,'_fx_webbert_dish_dragoninfernostew')
            return old_onputininventoryfn ~= nil and old_onputininventoryfn(this, owner, ...) or nil
        end
        local old_ondropfn = inst.components.inventoryitem.ondropfn
        inst.components.inventoryitem.ondropfn = function (this, ...)
            inst.Light:Enable(true)
            local fx = sugar:addEntRefer(inst, '_fx_webbert_dish_dragoninfernostew', SpawnPrefab('fx_webbert_misc'), true)
            ---@cast fx ent_fx
            local pt = inst:GetPosition()
            fx.entity:AddFollower()
            fx.Follower:FollowSymbol(inst.GUID, nil, 0, -200, 0)
            fx:fn_fx_webbert_misc('fx_webbert_firepie','fx_webbert_firepie','idle',true,false,nil,nil,pt)
            return old_ondropfn ~= nil and old_ondropfn(this, ...) or nil
        end
    end

    local old_OnLoad = inst.OnLoad
    inst.OnLoad = function (this, data)
        inst:DoTaskInTime(0,function ()
            if inst.components.inventoryitem and inst.components.inventoryitem.owner == nil and not inst:HasTag('INLIMBO') then
                inst.Light:Enable(true)
                local fx = sugar:addEntRefer(inst, '_fx_webbert_dish_dragoninfernostew', SpawnPrefab('fx_webbert_misc'), true)
                ---@cast fx ent_fx
                local pt = inst:GetPosition()
                fx.entity:AddFollower()
                fx.Follower:FollowSymbol(inst.GUID, nil, 0, -200, 0)
                fx:fn_fx_webbert_misc('fx_webbert_firepie','fx_webbert_firepie','idle',true,false,nil,nil,pt)
            end
        end)
        return old_OnLoad ~= nil and old_OnLoad(this, data) or nil
    end
end)

-- 料理: 轮回之心 死而复生
AddPlayerPostInit(function (inst)
    if not TheWorld.ismastersim then
        return inst
    end

    inst:ListenForEvent('death',function (this, data)
        if inst.HasStamp and inst:HasStamp('respawn_after_death') then
            inst:DoTaskInTime(3,function ()
                if inst and inst:IsValid() then
                    ---@type event_data_respawnfromghost
                    local event_data_respawnfromghost = { source = inst }
                    inst:PushEvent('respawnfromghost',event_data_respawnfromghost)

                    if inst.RemoveStamp then
                        inst:RemoveStamp('respawn_after_death')
                    end
                end
            end)
        end
    end)
end)